﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataModel.Entity;
using System.Collections.ObjectModel;
using System.Data;
using SmartMD.DataModel;

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class FollowupInfoDao : BaseGenericDaoImpl
    {
        public bool SaveOrUpdateObject(Object entity)
        {
            FollowupInfo followupInfo = (FollowupInfo)entity;
            DeleteObject(entity);
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" insert into");
            sb.Append(" DUO_FOLLOWUP_INFO (ID, PATIENTID, FOLLOWUPDATE, FOLLOWUPTIMES, FOLLOWUPER, ISFIRSTFOLLOWUP, BRIEFHISTORY, TREATMENT, BLOODPRESSURE, HEATRATE, BREATH, TEMPERATURE, OTHER, PATHOLOGYDIAGNOSE, PATHOLOGYCLASSIFICATION, PATHOLOGYLEVEL, PATHOLOGYPERIOD)");
            sb.Append(" values(");
            sb.Append(" '" + followupInfo.Id + "',");
            sb.Append(" '" + followupInfo.PatientId + "',");
          //  sb.Append(" '" + followupInfo.Diagnose + "',");
            sb.Append(" to_date ('" + followupInfo.FollowupDate.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' ) ,");
            sb.Append(" " + followupInfo.FollowupTimes.ToString() + ",");
            sb.Append(" '" + followupInfo.FollowUper + "',");
            sb.Append(" '" + followupInfo.IsFirstFollowUp.ToString() + "',");
            sb.Append(" '" + followupInfo.BriefHistory + "',");
            sb.Append(" '" + followupInfo.Treatment + "',");
            if (followupInfo.BloodPressure == null)
            {
                followupInfo.BloodPressure = "null";
            }
            sb.Append(" '" + followupInfo.BloodPressure.ToString() + "',");

            sb.Append(" " + followupInfo.Heatrate + ",");
            sb.Append(" " + followupInfo.Breath + ",");
            sb.Append(" " + followupInfo.Temperature + ",");
            sb.Append(" '" + followupInfo.Other + "',");
            sb.Append(" '" + followupInfo.PathologyDiagnose + "',");
            sb.Append(" '" + followupInfo.PathologyClassification + "',");
            sb.Append(" '" + followupInfo.PathologyLevel + "',");
            sb.Append(" '" + followupInfo.PathologyPeriod + "'");
            sb.Append(")");
            bool success = this.dbConn.Insert(sb.ToString());

            bool isSaved = true;
            if (followupInfo.DiagnoseList != null)
            {
                for (int j = 0; j < followupInfo.DiagnoseList.Count; j++)
                {
                    isSaved = SaveOrUpadateDiagnoses(followupInfo.DiagnoseList[j]);
                    success = success & isSaved;
                }
            }
            return success;
        }

        public bool SaveOrUpadateDiagnoses(DiagnoseGroup diagnosegroup)
        {
            DeleteDiagnose(diagnosegroup);

            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" insert into");
            sb.Append(" DUO_DIAGNOSE_GROUP (ID, FOLLOWUPID, DIAGNOSE)");
            sb.Append(" values(");
            sb.Append(" '" + diagnosegroup.Id + "',");
            sb.Append(" '" + diagnosegroup.FollowupId + "',");
            sb.Append(" '" + diagnosegroup.Diagnose + "'");
            sb.Append(")");
            bool success = this.dbConn.Insert(sb.ToString());
            return success;
        }

        public bool DeleteDiagnose(DiagnoseGroup entity)
        {
            if (entity == null)
                return true;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" delete ");
            sb.Append(" from DUO_DIAGNOSE_GROUP");
            sb.Append(" where ID = '" + entity.Id + "'");

            bool success = this.dbConn.Delete(sb.ToString());
            return success;
        }

        public bool DeleteObject(Object entity)
        {
            FollowupInfo followupInfo = (FollowupInfo)entity;
            if (followupInfo == null)
                return true;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" delete ");
            sb.Append(" from DUO_FOLLOWUP_INFO");
            sb.Append(" where ID = '" + followupInfo.Id + "'");

            bool success = this.dbConn.Delete(sb.ToString());
            return success;
        }

        public ObservableCollection<FollowupInfo> GetAllFollowupInfos()
        {
            ObservableCollection<FollowupInfo> itemList = new ObservableCollection<FollowupInfo>();
            FollowupInfo followupInfo = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_FOLLOWUP_INFO");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    followupInfo = new FollowupInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        followupInfo.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    itemList.Add(followupInfo);
                }
                return itemList;
            }
            else
                return null;
        }

        public ObservableCollection<FollowupInfo> GetFollowupInfos(List<KVP> kvps)
        {
            if (kvps == null || kvps.Count <= 0)
                return null;
            ObservableCollection<FollowupInfo> itemList = new ObservableCollection<FollowupInfo>();
            FollowupInfo followupInfo = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select t1.*, t2.NAME");
            sb.Append(" from DUO_FOLLOWUP_INFO t1 , DUO_PATIENT t2");
            sb.Append(" where t1.PATIENTID = t2.PATIENTID ");
            for (int k = 0; k < kvps.Count; k++)
            {
                switch (kvps[k].key.Trim())
                {
                    case "姓名":
                        sb.Append(" and t2.NAME ='" + kvps[k].value.Trim() + "'");
                        break;
                    case "随访人":
                        sb.Append(" and t1.FOLLOWUPER ='" + kvps[k].value.Trim() + "'");
                        break;
                    case "疾病诊断":
                        sb.Append(" and t1.DIAGNOSE ='" + kvps[k].value.Trim() + "'");
                        break;
                    case "随访周次":
         
                        sb.Append(" and t1.FOLLOWUPTIMES ='" + kvps[k].value.Trim() + "'");
                        break;
                }
            }

            try
            {
                DataTable dt = this.dbConn.Select(sb.ToString());
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        followupInfo = new FollowupInfo();
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            string clmName;
                            clmName = dt.Columns[i].ColumnName;
                            followupInfo.setValue(clmName, dt.Rows[j][clmName]);
                        }
                        itemList.Add(followupInfo);
                    }
                    return itemList;
                }
                else
                    return null;
            }
            catch {
                return null;
            }
           
        }
        public List<DiagnoseGroup> GetDiagnoseGroupsByFollowupId(string followupid)
        {
            List<DiagnoseGroup> itemList = new List<DiagnoseGroup>();
            DiagnoseGroup diagnoseInfo = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_DIAGNOSE_GROUP");
            sb.Append(" where FOLLOWUPID ='" + followupid + "'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    diagnoseInfo = new DiagnoseGroup();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        diagnoseInfo.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    itemList.Add(diagnoseInfo);
                }
                return itemList;
            }
            else
                return null;
        }

        public ObservableCollection<FollowupInfo> GetFollowupInfoByPatientId(string patientId)
        {
            ObservableCollection<FollowupInfo> itemList = new ObservableCollection<FollowupInfo>();
            FollowupInfo followupInfo = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_FOLLOWUP_INFO");
            sb.Append(" where PATIENTID ='" + patientId + "'");
            sb.Append("  order by followupdate desc ");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    followupInfo = new FollowupInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        followupInfo.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    List<DiagnoseGroup> diagnoseList = GetDiagnoseGroupsByFollowupId(followupInfo.Id);
                    followupInfo.DiagnoseList = diagnoseList;
                    itemList.Add(followupInfo);
                }
                return itemList;
            }
            else
                return null;
        }
        /**
         * 功能：获取首次随访信息
         * 
         * */
        public FollowupInfo GetFirstFollowupInfoByPatientId(string pId)
        {
            FollowupInfo followupInfo = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_FOLLOWUP_INFO");
            sb.Append(" where PATIENTID ='" + pId + "'");
            sb.Append(" and rownum = 1 ");
            sb.Append(" order by FOLLOWUPDATE asc ");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    followupInfo = new FollowupInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        followupInfo.setValue(clmName, dt.Rows[j][clmName]);
                    }                   
                }
                return followupInfo;
            }
            else
                return null;
        }
    }
}
